package com.whitedust.top60;

/**
 *
 * @author A
 * @since 2025/11/11 12:54
 */

public class Top59 {
    public String reverseWords(String s) {
        String trim = s.trim();
        int i = trim.length() - 1;
        int j = trim.length() - 1;
        StringBuilder sb = new StringBuilder();
        while (i >= 0) {
            //找到第一个空格
            while (i >= 0 && trim.charAt(i) != ' ') i--;
            sb.append(trim, i + 1, j + 1).append(" ");
            //跳过剩下的空格
            while (i >= 0 && trim.charAt(i) == ' ') i--;
            //指向下个词的词尾
            j = i;
        }
        return sb.toString().trim();
    }
}
